package com.gs.api.projects.shejiao.web;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gs.api.core.Result;
import com.gs.api.projects.bzapplets.service.BzConfigService;
import com.gs.api.projects.shejiao.entity.ManheConfig;
import com.gs.api.projects.shejiao.entity.TMemberUser;
import com.gs.api.projects.shejiao.entity.Withdrawal;
import com.gs.api.projects.shejiao.service.IManheConfigService;
import com.gs.api.projects.shejiao.service.ITMemberUserService;
import com.gs.api.projects.shejiao.service.IWithdrawalService;
import com.gs.api.util.Arith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * <p>
 * 用户提现表 前端控制器
 * </p>
 *
 * @author guoshao
 * @since 2021-10-17
 */
@RestController
@RequestMapping("{appid}/withdrawal")
public class WithdrawalController {

    @Autowired
    private IWithdrawalService iWithdrawalService;

    @Autowired
    private Snowflake snowflake;

    @Autowired
    private ITMemberUserService itMemberUserService;


    @Autowired
    private IManheConfigService iManheConfigService;

    @GetMapping("/save")
    public Result save(@PathVariable String appid, Withdrawal withdrawal){

        //TODO 验证提现次数
        ManheConfig manheConfig = iManheConfigService.getOne(new QueryWrapper<ManheConfig>().eq("appid", appid));
        if(null == manheConfig){
            return Result.failure("小程序配置信息错误");
        }
        if(manheConfig.getMinMoney().compareTo(withdrawal.getMoney()) == 1){
            return Result.failure("提现失败,最低提现金额为："+manheConfig.getMinMoney());
        }

        Date date = new Date();
        String format = DateUtil.format(date,"yyyy-MM-dd");
        int count = iWithdrawalService.count(
                new QueryWrapper<Withdrawal>().between("apply_time", format + " 00:00:00", format + " 23:59:59")
        .between("status",1,2));
        if(count >= manheConfig.getDayCount()){
            return Result.failure("提现失败,每天可提现"+manheConfig.getDayCount()+"次");
        }

        //TODO 查询用户当前余额
        TMemberUser memberUser = itMemberUserService.getOne(new QueryWrapper<TMemberUser>().eq("user_id", withdrawal.getUserId()));

        if(null == memberUser){
            return Result.failure("提现失败,无效会员");
        }

        //提现金额 + 冻结余额 是否大于 现有余额
        if(withdrawal.getMoney().compareTo(memberUser.getBalance()) == 1){
            return Result.failure("提现失败,佣金不足");
        }

        // TODO 修改冻结余额和现有余额
        memberUser.setBalance(memberUser.getBalance().subtract(withdrawal.getMoney()));
        memberUser.setFrozenBalance(withdrawal.getMoney().add(memberUser.getFrozenBalance()));
        boolean b = itMemberUserService.updateById(memberUser);

        if(b){
            withdrawal.setAppid(appid);
            withdrawal.setStatus(1);
            withdrawal.setApplyTime(new Date());
            withdrawal.setFlowNo(String.valueOf(snowflake.nextId()));
            iWithdrawalService.save(withdrawal);
            return Result.success();
        }

        return Result.failure("提现失败，请联系客服");
    }

}
